package com.heu.blood.dict.controller;

import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.R;
import com.heu.blood.dict.entity.DictDataEntity;
import com.heu.blood.dict.service.DictDataLoaderService;
import com.heu.blood.dict.service.DictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 
 *
 * @author lrz
 * @email lrz@heu.com
 * @date 2023-07-30 21:48:23
 */
@RestController
@RequestMapping("dict/dictdata")
public class DictDataController {
    @Autowired
    private DictDataService dictDataService;

    @Autowired
    private DictDataLoaderService dictDataLoaderService;

    /**
     * 分页条件查询字典数据项
     */
    @RequestMapping("/list")
    @ControllerLogAnnotation(module = "字典项信息", operate = "分页条件查询字典数据项")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = dictDataService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 重新刷新所有字典
     */
    @RequestMapping("/reload")
    @ControllerLogAnnotation(module = "字典项信息", operate = "重新刷新所有字典")
    public R reloadAllDictionaries(@RequestParam Map<String, Object> params){
        dictDataLoaderService.reloadAllDictionaries(params);
        return R.ok("刷新字典成功");
    }
    /**
     * 条件查询字典数据项信息
     * @param dictDataEntity
     * @return
     */
    @GetMapping("/query")
    @ControllerLogAnnotation(module = "字典项信息", operate = "条件查询字典数据项信息")
    public R query(@RequestBody DictDataEntity dictDataEntity){
        List<DictDataEntity> dictDataEntityList = dictDataService.query(dictDataEntity);
        if (dictDataEntityList.size() == 0 && dictDataEntityList.isEmpty()){
            return R.error("未查询到相关字典信息！");
        }
        return R.ok().put("data", dictDataEntityList);
    }

    /**
     * 根据id搜索字典数据项
     */
    @RequestMapping("/info/{id}")
    @ControllerLogAnnotation(module = "字典项信息", operate = "根据id搜索字典数据项")
    public R info(@PathVariable("id") Long id){
		DictDataEntity dictData = dictDataService.getById(id);

        return R.ok().put("dictData", dictData);
    }

    /**
     * 保存字典数据项
     */
    @RequestMapping("/save")
    @ControllerLogAnnotation(module = "字典项信息", operate = "保存字典数据项")
    public R save(@RequestBody DictDataEntity dictData){
		dictDataService.save(dictData);

        return R.ok();
    }

    /**
     * 前端获得所有的dictData进行code和value的转换
     * @return
     */
    @GetMapping("/getAllDictData")
    @ControllerLogAnnotation(module = "字典项信息", operate = "前端获得所有的dictData进行code和value的转换")
    public R getAllDictData(){
        Map<String, Map<String,String[]>> res = dictDataService.getAllDictData();
        return R.ok().put("data", res);
    }

    /**
     * 修改字典数据项
     */
    @RequestMapping("/update")
    @ControllerLogAnnotation(module = "字典项信息", operate = "修改字典数据项")
    public R update(@RequestBody DictDataEntity dictData){
		dictDataService.updateById(dictData);

        return R.ok();
    }

    /**
     * 删除字典数据项
     */
    @RequestMapping("/delete")
    @ControllerLogAnnotation(module = "字典项信息", operate = "删除字典数据项")
    public R delete(@RequestBody Long[] ids){
		dictDataService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 根据code查询appendix
     */
    @GetMapping("/queryByCode")
    @ControllerLogAnnotation(module = "字典项信息", operate = "根据code查询appendix")
    public R queryByCode(String code){
        DictDataEntity dictDataEntity = dictDataService.queryByCode(code);

        return R.ok().put("data", dictDataEntity);
    }

}
